package org.msb.accentis.gui.views;

import static java.awt.GridBagConstraints.BOTH;
import static java.awt.GridBagConstraints.CENTER;
import static java.awt.GridBagConstraints.EAST;
import static java.awt.GridBagConstraints.HORIZONTAL;
import static java.awt.GridBagConstraints.NONE;
import static java.awt.GridBagConstraints.WEST;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.msb.accentis.data.DataManagerFactory;
import org.msb.accentis.data.Database;
import org.msb.accentis.data.IDataManagerFactoryListener;
import org.msb.accentis.data.QueryParameterMap;
import org.msb.accentis.data.pojo.Account;
import org.msb.accentis.data.pojo.Transaction;
import org.msb.accentis.gui.InsetsConstants;
import org.msb.accentis.gui.Resources;
import org.msb.accentis.gui.models.PojoComboBoxModel;
import org.msb.accentis.gui.models.TransactionTableModel;

public class TransactionsView extends AbstractView implements IDataManagerFactoryListener {

	private JComboBox<Account> accountComboBox;

	private JTable transactionTable;

	private TransactionEditor editor;

	public TransactionsView() {
		super("transactions");

		DataManagerFactory.getInstance().addDataManagerFactoryListener(this);
	}

	@Override
	public String getActionResourceKey() {
		return "action.view.transactions";
	}

	@Override
	protected JComponent getViewComponent() {
		JTabbedPane component = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
		component.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

		component.addTab(Resources.getInstance().getString("transactionsview.tab.register.title"), createRegisterTab());
		// Downloaded Transactions Tab goes here.

		return component;
	}

	private JComponent createRegisterTab() {
		JPanel tab = new JPanel(new GridBagLayout());
		tab.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

		tab.add(new JLabel(Resources.getInstance().getString("transactionsview.tab.register.heading.caption")), new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0,
				WEST, NONE, InsetsConstants.ZERO_INSETS, 0, 0));
		tab.add(new JLabel(Resources.getInstance().getString("transactionsview.tab.register.accounts.caption")), new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
				EAST, NONE, InsetsConstants.FIVE_ON_LEFT_INSETS, 0, 0));
		tab.add(getAccountComboBox(), new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, EAST, HORIZONTAL, InsetsConstants.FIVE_ON_LEFT_INSETS, 0, 0));

		tab.add(new JScrollPane(getTransactionTable()), new GridBagConstraints(0, 1, 3, 1, 0.0, 1.0, CENTER, BOTH, InsetsConstants.FIVE_ON_TOP_INSETS, 0, 0));

		tab.add(getTransactionEditor(), new GridBagConstraints(0, 2, 3, 1, 0.0, 0.0, CENTER, BOTH, InsetsConstants.FIVE_ON_TOP_INSETS, 0, 0));

		return tab;
	}

	protected JComboBox<Account> getAccountComboBox() {
		if (null == accountComboBox)
			createAccountComboBox();

		return accountComboBox;
	}

	private void createAccountComboBox() {
		accountComboBox = new JComboBox<Account>(new PojoComboBoxModel<Account>("Account.getAllAccounts", Account.class, false));
		accountComboBox.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				TransactionTableModel model = (TransactionTableModel) getTransactionTable().getModel();
				Account account = (Account) e.getItem();
				model.setAccount(account);
				getTransactionEditor().setAccount(account);
			}
		});
	}

	protected JTable getTransactionTable() {
		if (null == transactionTable)
			createTransactionTable();

		return transactionTable;
	}

	private void createTransactionTable() {
		transactionTable = new JTable(new TransactionTableModel());
		transactionTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				if (!e.getValueIsAdjusting()) {
					int row = transactionTable.getSelectedRow();
					if (-1 != row) {
						TransactionTableModel model = (TransactionTableModel) transactionTable.getModel();
						Transaction value = model.getTransactionAt(row);
						getTransactionEditor().setTransaction(value);
					} else
						getTransactionEditor().setTransaction(null);
				}
			}
		});
		transactionTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		transactionTable.setShowVerticalLines(false);
	}

	protected TransactionEditor getTransactionEditor() {
		if (null == editor)
			createTransactionEditor();

		return editor;
	}

	private void createTransactionEditor() {
		editor = new TransactionEditor();
	}

	@Override
	public void databaseChanged(Database database) {
		if (null != database) {
			((PojoComboBoxModel<Account>) getAccountComboBox().getModel()).reload(QueryParameterMap.NO_PARAMETERS);

			if (0 < getAccountComboBox().getItemCount())
				getAccountComboBox().setSelectedIndex(0);
			else
				getAccountComboBox().setSelectedIndex(-1);
		}
	}
}
